package checker.algorithms.sort;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public abstract class SortBase {
    // sort，留给子类实现
    public abstract void sort(Comparable[] a);

    // 判断v是否小于w
    public static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    // 交换a数组中的i和j位置的元素
    protected static void exch(Comparable[] a, int i, int j) {
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    // 打印数组
    protected static void show(Comparable[] a) {
        for (Comparable comparable : a) {
            System.out.print(comparable + " ");
        }
        System.out.println();
    }

    // 判断是否有序（升序）
    protected static boolean isSorted(Comparable[] a) {
        for (int i = 1; i < a.length; i++) {
            if (less(a[i], a[i - 1])) {
                return false;
            }
        }
        return true;
    }

    // 读入测试数据，执行sort
    public void run() {
        BufferedReader bufferedReader = null;
        String s = null;
        try {
            bufferedReader = new BufferedReader(new FileReader("src/main/resources/tiny.txt"));
            s = bufferedReader.readLine();
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (s != null) {
            String[] in = s.split(" ");
            sort(in);
            System.out.println(Arrays.toString(in));
        } else {
            System.out.println("输入有误！");
        }

    }

}
